﻿using System.Web.Mvc;
using DBF = DBFile.Core;

namespace DBFile.Core.Web.Mvc
{
    public static class Extensions
    {
        public static DBFileResult FileFromDB(this Controller controller, string fileName, string contentType)
        {
            return new DBFileResult(fileName, contentType);
        }

        public static FileContentResult FileFromDB(this Controller controller, string fileName)
        {
            HttpPostedFile file = DBF.DBFile.OpenFile(fileName);

            byte[] buffer = new byte[file.ByteCount];
            try
            {
                file.InputStream.Read(buffer, 0, buffer.Length);
            }
            finally
            {
                file.InputStream.Close();
            }

            return new FileContentResult(buffer, file.ContentType) { FileDownloadName = fileName };          
        }

    }
}
